/**
 * 
 */
package com.acminds.fotoporter.utils;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Collection;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.media.jai.InterpolationBicubic2;
import javax.media.jai.JAI;
import javax.media.jai.OpImage;
import javax.media.jai.RenderedOp;

import com.acminds.fotoporter.exceptions.FotoPorterRuntimeException;
import com.sun.media.jai.codec.ByteArraySeekableStream;

/**
 * @author MANSUR
 *
 */
public class Utils {
	
	@SuppressWarnings("rawtypes")
	public static boolean isEmpty(Object value) {
		if (value == null)
			return true;
		if (value instanceof String) {
			return (((String) value).trim().length() == 0);
		}
		if (value instanceof Collection) {
			return (((Collection) value).size() == 0);
		}
		return false;
	}
	
	public static Date today() {
		return new Date();
	}
	
	public static byte[] scale(byte[] fileData, int width) {        
        try {
        	ByteArrayInputStream in = new ByteArrayInputStream(fileData);
            BufferedImage img = ImageIO.read(in);
            AffineTransform af = new AffineTransform();
            af.scale(width, ImageUtils.getScaledHeight(img, width));

            AffineTransformOp operation = new AffineTransformOp(af, AffineTransformOp.TYPE_BICUBIC);
            BufferedImage bufferedThumb = operation.filter(img, null);

            return ImageUtils.toByteArray(bufferedThumb);
        } catch (Exception e) {
        	e.printStackTrace();
            throw new FotoPorterRuntimeException("IOException while scaling Image", e);
        }
    }
	
	
	
	public static byte[] scaleJAI(byte[] fileData, int width, int height) {        
        try {
        	ByteArraySeekableStream s = new ByteArraySeekableStream(fileData);
  
            RenderedOp objImage = JAI.create("stream", s);
            ((OpImage) objImage.getRendering()).setTileCache(null);
  
            if (objImage.getWidth() == 0 || objImage.getHeight() == 0) {
                System.out.println("graphic size is zero");
                return null;
            }
  
            float xScale = (float) (width * 1.0) / objImage.getWidth();
            float yScale = (float) (height * 1.0) / objImage.getHeight();
            float scale = xScale;
            if (xScale > yScale) {
                scale = yScale;
            }
  
            ParameterBlock pb = new ParameterBlock();
            pb.addSource(objImage); // The source image
            pb.add(scale); // The xScale
            pb.add(scale); // The yScale
            pb.add(0.0F); // The x translation
            pb.add(0.0F); // The y translation
            pb.add(new InterpolationBicubic2(5)); // The interpolation
  
            objImage = JAI.create("scale", pb, null);
            ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
            JAI.create("encode", objImage, out, "JPEG");
            return out.toByteArray();
        } catch (Exception e) {
        	e.printStackTrace();
            throw new FotoPorterRuntimeException("IOException while scaling Image");
        }
    }

}
